/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */

package com.pekall.myDictionary.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.widget.Toast;

public class ShareUtil {

    private final String PREFILENAME = "mydictionary";

    private Context nContext;

    private SharedPreferences nSharedPreferences;

    public ShareUtil(Context context) {
        // 将传进来的activity赋予nContext
        nContext = context;
        // 获取用于存储的文件
        nSharedPreferences = context.getSharedPreferences(PREFILENAME, Context.MODE_PRIVATE);
    }

    public void showMessage(String message) {
        // 用于显示toast信息
        Toast.makeText(nContext, message, Toast.LENGTH_LONG).show();
    }

    public void saveString(String key, String value) {
        nSharedPreferences.edit().putString(key, value).commit();
    }

    // 获取字符串
    public String getString(String key, String... defValue) {
        if (defValue.length > 0)
            return nSharedPreferences.getString(key, defValue[0]);
        else
            return nSharedPreferences.getString(key, "");
    }

    // 暂时不知道该函数用处
    public static String getLeftString(String s, String separater) {
        int indexOf = s.indexOf(separater);
        if (indexOf > -1)
            return s.substring(0, indexOf);
        else
            return s;
    }

    // 保持声明提示
    public void ok() {
        saveString("pronouncement", "1");
    }

    public void cancle() {
        saveString("pronouncement", "0");
    }

}
